package com.gx.smart.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.gx.smart.base.loading.LoadingView;

import org.greenrobot.eventbus.EventBus;

/**
 * 基础fragment
 */

public abstract class BaseFragmentV2 extends Fragment {
    protected View mView;
    protected LoadingView mLoadingInitView;
    protected View mNoDataView;
    protected TextView mTxtTitle;
    protected Toolbar mToolbar;
    protected RelativeLayout mViewStubContent;
    private ViewStub mViewStubToolbar;
    private ViewStub mViewStubInitLoading;
    private ViewStub mViewStubNoData;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_root1, container, false);
        initView(mView);
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initContent();
        initData();
    }

    /**
     * 初始化view
     *
     * @param view
     */
    protected void initView(@NonNull View view) {

    }

    /**
     * 初始化内容
     */
    protected void initContent() {
        initCommonView(mView);
    }

    public void initCommonView(View view) {
        mViewStubToolbar = view.findViewById(R.id.view_stub_toolbar);
        mViewStubContent = view.findViewById(R.id.view_stub_content);
        mViewStubInitLoading = view.findViewById(R.id.view_stub_init_loading);
        mViewStubNoData = view.findViewById(R.id.view_stub_nodata);

        if (enableToolbar()) {
            mViewStubToolbar.setLayoutResource(onBindToolbarLayout());
            View viewTooBar = mViewStubToolbar.inflate();
            initTooBar(viewTooBar);
        }
        initContentView(mViewStubContent);
    }

    public void initContentView(ViewGroup root) {
        LayoutInflater.from(getContext()).inflate(onBindLayout(), root, true);
    }

    public abstract int onBindLayout();

    public boolean enableToolbar() {
        return true;
    }

    protected void initTooBar(View view) {
        mToolbar = view.findViewById(R.id.toolbar);
        mTxtTitle = view.findViewById(R.id.title);
        if (mToolbar != null) {
            ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((AppCompatActivity) getActivity()).onBackPressed();
                }
            });
        }
        if (mTxtTitle != null) {
            mTxtTitle.setText(getToolbarTitle());
        }
    }

    public abstract String getToolbarTitle();

    /**
     * 网络请求数据
     */
    protected void initData() {

    }

    public int onBindToolbarLayout() {
        return R.layout.common_toolbar;
    }


    public void showInitLoadView(boolean show) {
        if (mLoadingInitView == null) {
            View view = mViewStubInitLoading.inflate();
            mLoadingInitView = view.findViewById(R.id.loadingView);
        }
        mLoadingInitView.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    public void showNoDataView(boolean show) {
        if (mNoDataView == null) {
            View view = mViewStubNoData.inflate();
            mNoDataView = view.findViewById(R.id.view_no_data);
        }
        mNoDataView.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    protected void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    protected void unregisterEventBus() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
